package org.itboys.core.service.mongo.front;

import lombok.Data;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.ExceptionEnum;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayAddress;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/10/15
 * 描述：地址
 */
@Data
@Service
public class SundayFrontAddressService extends BaseMongoService<SundayAddress> {
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayAddress> getEntityClass() {
        return SundayAddress.class;
    }

    /**
     * 获取用户所有收货地址,不分页
     * @param memberId
     * @return
     */
    public List<SundayAddress> selectAddress(Long memberId){
        Map<String,Object> param =new HashMap<String, Object>();
        param.put("memberId", memberId);
        param.put("isDeleted", SundayCommonConstants.NO);
        //有限排序默认地址
        param.put("sort","isDefault");
        param.put("order","DESC");
        //SundayMember member=memberService.getById(memberId);
        List<SundayAddress> adds = super.list(param);

        return adds;
    }

    /**
     * 设置默认地址
     *
     * @param addressId
     * @param memberId
     */
    public void setDefaultAddress(Long addressId, Long memberId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId", memberId);
        List<SundayAddress> addresses = super.list(param);
        for (SundayAddress address : addresses) {
            if (address.getId() == addressId) {
                address.setIsDefault(SundayCommonConstants.YES);
                super.updateExceptEmpty(address.getId(), address);
            } else {
                address.setIsDefault(SundayCommonConstants.NO);
                super.updateExceptEmpty(address.getId(), address);
            }
        }
    }

    /**
     * 新增
     *
     * @param id
     * @param memberId
     * @param receiveName
     * @param receiveMobile
     * @param provinceName
     * @param cityName
     * @param districtName
     * @param receiveAddress
     * @param isDefault
     * @return
     */
    public SundayAddress saveAddress(Long id, Long memberId, String receiveName, String receiveMobile,
                                     Long provinceId,String provinceName, Long cityId,String cityName, Long districtId,String districtName,
                                     String receiveAddress, Integer isDefault) {
        SundayAddress address = null;
        if (id == null || id == 0l) {
            address = new SundayAddress(
                    id, memberId, receiveName, receiveMobile,
                    provinceId,provinceName,cityId, cityName,districtId, districtName,
                    receiveAddress, isDefault);
            if (isDefault == SundayCommonConstants.YES) {
                Map<String, Object> param = new HashMap<String, Object>();
                param.put("isDeleted", SundayCommonConstants.NO);
                param.put("memberId", memberId);
                List<SundayAddress> addresses = super.list(param);
                for (SundayAddress oldAddress : addresses) {
                    oldAddress.setIsDefault(SundayCommonConstants.NO);
                    super.updateExceptEmpty(oldAddress.getId(), oldAddress);
                }
            }
            super.save(address);
        } else {
            if (isDefault == SundayCommonConstants.YES) {
                Map<String, Object> param = new HashMap<String, Object>();
                param.put("isDeleted", SundayCommonConstants.NO);
                param.put("memberId", memberId);
                List<SundayAddress> addresses = super.list(param);
                for (SundayAddress oldAddress : addresses) {
                    oldAddress.setIsDefault(SundayCommonConstants.NO);
                    super.updateExceptEmpty(oldAddress.getId(), oldAddress);
                }
            }
            address = super.getById(id);
            super.updateExceptEmpty(id, new SundayAddress(id, memberId, receiveName, receiveMobile,
                    provinceId,provinceName, cityId,cityName,districtId, districtName, receiveAddress, isDefault));
        }
        return address;
    }

    /**
     * 获取默认的地址
     *
     * @param memberId
     * @return
     */
    public SundayAddress getDefaultAddress(Long memberId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("isDefault", SundayCommonConstants.YES);
        param.put("memberId", memberId);
        List<SundayAddress> addes = super.list(param);
        if (ListUtil.isNotNull(addes)) return addes.get(0);
        return null;
    }

    /**
     * 删除收货地址
     * @param addressId
     * @param memberId
     */
    public void deleteAddress(Long addressId,Long memberId){
        //2017年6月20日，新增，验证签名
        SundayAddress address=super.getById(addressId);
        if(address.getIsDefault()==SundayCommonConstants.YES){
            throw new ErrorException(ExceptionEnum.ADDRESS_DEFAULT_EXCEPTION.getValue());
        }
        super.update(addressId,"isDeleted", SundayCommonConstants.YES);
    }

}
